package com.example.newstudent.controller;

import com.example.newstudent.entity.*;
import com.example.newstudent.service.*;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.example.newstudent.entity.optional_course.summarizeCourses;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    userService userService;
    @Autowired
    studentService studentService;
    @Autowired
    lessonService lessonService;
    @Autowired
    optional_courseService optional_courseService;
    @Autowired
    bulletinService bulletinService;
    @PostMapping("/Login")
    public Result Login(@RequestBody user user){
        System.out.println("账号为"+user.getAccount()+"的管理员登录");
        user login = userService.Login(user);
        if (login != null) {
            System.out.println("登陆成功");
            return Result.success(login);
        }else {
            System.out.println("登录失败");
            return Result.error("密码错误");
        }
    }
    @PostMapping("/userSelect")
    public Result userSelect(@RequestBody user user){
        System.out.println("账号为"+user.getAccount()+"的管理员查看人员");
        return Result.success(userService.userSelect());
    }
    @PostMapping("/userUpdate")
    public Result userUpdate(@RequestBody student student){
        System.out.println("修改学号为"+student.getId()+"的同学的信息");
        return Result.success(studentService.updatemessage(student));
    }
    @PostMapping("/userDelete")
    public  Result userDelete(@RequestBody student student){
        System.out.println("删除学号为"+student.getId()+"同学的信息");
        userService.userDelete(student);
        return Result.success();
    }
    @PostMapping("/lessonSelect")
    public Result lessonSelect(){
        System.out.println("管理员查看课表");
        return Result.success(lessonService.selectAll());
    }
    @PostMapping("/lessonByStudentId")
    public Result lessonByStudentId(@RequestParam int id){
        System.out.println("管理员查看学号为"+id+"同学的课程");
        return Result.success(lessonService.lessonByStudentId(id));
    }
    @PostMapping("/lessonUpdate")
    public Result lessonUpdate(@RequestBody lesson lesson){
        System.out.println("修改课程"+lesson.getName()+"的信息");
        return Result.success(lessonService.lessonUpdate(lesson));
    }
    @PostMapping("/punishBulletin")
    public Result punishBulletin(@RequestBody bulletin bulletin){
        System.out.println("管理员发布公告");
        return Result.success(bulletinService.punishBulletin(bulletin));
    }
    @PostMapping("/deleteBulletin")
    public Result deleteBulletin(@RequestBody bulletin bulletin){
        System.out.println("删除主题为"+bulletin.getTopic()+"的公告");
        bulletinService.deleteBulletin(bulletin.getId());
        return Result.success();
    }
    @PostMapping("/updatemessage")
    public Result updatemessage(@RequestBody user user){
        System.out.println("账号为"+user.getAccount()+"的管理员修改信息");
        if (user.getAccount().isEmpty()
                ||user.getPin().isEmpty()
                ||user.getTelephone().isEmpty() ) {
            return Result.error("表单项不能为空");
        }else
            return  Result.success(userService.updatemessage(user));
    }
    @GetMapping("/optional")
    public Result optional(@RequestParam String name){
        System.out.println("管理员查看选修课");
        if (name.isEmpty()) {
            name=null;
        }
        List<optional_course>list = new ArrayList<>();
        List<optional_course> optional_courses = userService.optional(name);
        Map<String, Map<String, Object>> weeksCount = summarizeCourses(optional_courses);
        for (Map.Entry<String, Map<String, Object>> entry : weeksCount.entrySet()) {
            optional_course optional_course = new optional_course();
            optional_course.setName(entry.getKey());
            optional_course.setTeacher((String) entry.getValue().get("teacher"));
            optional_course.setWeeks((String) entry.getValue().get("weeks"));
            optional_course.setAddress((String) entry.getValue().get("address"));
            optional_course.setSection((Integer) entry.getValue().get("section"));
            optional_course.setDay((Integer) entry.getValue().get("day"));
            list.add(optional_course);
        }
        System.out.println(optional_courses);
        return Result.success(list);
    }
    @GetMapping("/deleteOptional")
    public Result deleteOptional(@RequestParam String name){
        userService.deleteOptional(name);
        return Result.success();
    }
    @PostMapping("/addOptional")
    public Result addOptional(@RequestBody optional_course optional_course){
        System.out.println("管理员增加选修课");
        userService.addOptional(optional_course);
        return Result.success();
    }
    @GetMapping("/selectById")
    public Result selectById(@RequestParam String id){
        System.out.println("管理员查询学号为"+id+"的同学");
        return Result.success(studentService.selectById(id));
    }
//    @GetMapping("/selectByName")
//    public Result selectByName(@RequestParam String name){
//        System.out.println("管理员查看单个课程");
//        optional_course o
//        return Result.success();
//    }
}
